<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>修改密码</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- 可选的Bootstrap主题文件（一般不用引入） -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="/css/font-awesome.css">
    <link rel="stylesheet" href="/css/base.css">
    <link rel="stylesheet" href="/css/djs.css">

    <script src="/json/CityJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/json/ProJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/json/DistrictJson.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/user.js"></script>
    <script type="text/javascript" src="/js/base.js"></script>
</head>
<body>
<div id="top">

</div>
<div class="container">
    <div id="asideAffix" class="aside" data-spy="affix">
    </div>
    <div class="content centers editcontent" style="margin-top: 0;">
        <form role="form" id="editForm" class="form-horizontal">
            <div class="form-group">
                <div class="col-sm-12">
                    <label for="originPassword" class="col-sm-2">原始密码：</label>
                    <input type="password" id="originPassword" required class="col-sm-4"><span id="originPasswordTip" style="display:none;color:red;"></span>
                </div>

                <div class="col-sm-12">
                    <label for="password1" class="col-sm-2">新密码：</label>
                    <input type="password" id="password1" required class="col-sm-4"><span id="password1Tip" style="display:none;color:red;"></span>
                </div>

                <div class="col-sm-12" id="passwordDiv">
                    <label for="password2" class="col-sm-2">重复新密码：</label>
                    <input type="password" id="password2" required class="col-sm-4"><span id="password2Tip" style="display:none;color:red;"></span>
                </div>
                <div>
                    <button type="button" class="col-sm-2 btn btn-primary" id="rollback"
                            style="border-color: #ffd632;background: #ffd632 linear-gradient(to bottom,#ffd632 0,#ffd632 100%);">
                        返回
                    </button>
                    <button class="col-sm-2 btn btn-primary" id="create"
                            style="border-color: #ffd632;background: #ffd632 linear-gradient(to bottom,#ffd632 0,#ffd632 100%);">
                        保存
                    </button>
                </div>
                </div>
        </form>
    </div>
</div>
</body>

<script type="text/javascript">

    var error = false;

    $(document).ready(function () {

        $("#originPassword").blur(function () {

            var originPassword = $("#originPassword").val();
            if (originPassword == '') {
                showError('originPassword', '密码不能为空');
                error = true;
                return;
            }

            $.ajax({
                url: '/user/checkPassword',// 跳转到 action
                headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
                data: JSON.stringify({
                    password: $("#originPassword").val()
                }),
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    if (data.statusCode == 0) {
                        $("#originPassword").css({"border-color":"green"});
                        $("#originPasswordTip").css({"display":"none"});
                    } else {
                        showError('originPassword', '密码错误');
                        error = true;
                    }
                },
                error: function () {
                    // view("异常！");
                    alert("服务器感冒了1！");
                }
            });
        });

        $("#password1").blur(function () {
            var password1 = $("#password1").val();
            if (password1 == '') {
                showError('password1', '新密码不能为空');
                error = true;
            }
            else {
                $("#password1").css({"border-color": "green"});
                $("#password1Tip").css({"display":"none"});
            }
        });

        $("#password2").blur(function () {
            var newpass = $("#password1").val();
            if (newpass == '') {
                showError('password1', '新密码不能为空');
                error = true;
                return;
            }

            var password2 = $("#password2").val();
            if (password2 != newpass) {
                showError('password2', '与输入的新密码不一致');
                error = true;
            }
            else {
                $("#password2").css({"border-color": "green"});
                $("#password2Tip").css({"display": "none"});
            }
        });

        $("#submit").click(function (event) {


            event.preventDefault();
            return false;
        });
    });

    function showError(formSpan, errorText) {
        $("#" + formSpan).css({"border-color": "red"});
        $("#" + formSpan + "Tip").empty();
        $("#" + formSpan + "Tip").append(errorText);
        ;
        $("#" + formSpan + "Tip").css({"display": "inline"});
    }

    var orgId = 0;
    $("#editForm").submit(
            function () {
                if (!areYouSure("")) {
                    return false;
                }else{
                    save();
                    return false;
                }

            }
    );

    function save() {
        error = false;
        $("#originPassword").blur();
        $("#password1").blur();
        $("#password2").blur();

        if (!error) {
            $.ajax({
                url: '/user/updatePassword',// 跳转到 action
                headers: {'Content-type': 'application/json;charset=UTF-8'},//传入数据数据格式 json
                data: JSON.stringify({
                    password: $("#password2").val()
                }),
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    if (data.statusCode == 0) {
                        alert("修改密码成功！");
                        back();
                    } else {
                        showError('originPassword', '密码错误');
                        error = true;
                    }
                },
                error: function () {
                    // view("异常！");
                    alert("服务器感冒了1！");
                }
            });
        }
    }

    $("#rollback").click(function () {
        back()
    });

    function back() {
        location.href = "/index.html";
    }
</script>
</html>